package com.b2b.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.model.CartDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Cart;
import com.b2b.model.entity.goods.Goods;
import com.b2b.model.entity.goods.Stock;
import com.b2b.service.CartService;
import com.b2b.service.ShopService;
import com.b2b.service.goodsService.GoodsService;
import com.b2b.service.goodsService.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
@PropertySource("classpath:config.properties")
public class CartController {

    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    @Autowired
    CartService cartService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    StockService stockService;

    @Autowired
    ShopService shopService;

    @PostMapping("/add")
    public ResponseBean add(@RequestBody Cart cart){
//        Cart cart = new Cart();
//        cart.setCompanyId(cartDto.getCompanyId());
//        cart.setStockId(cartDto.getStockId());
        Cart cartTemp = new Cart();
        cartTemp.setCompanyId(cart.getCompanyId());
        Integer count = cartService.selectCount(cartTemp);
        if(count > 0){
            cartTemp = cartService.selectOne(cartTemp);
            cartTemp.setNumber(cartTemp.getNumber() + cart.getNumber());
            cartService.updateByPrimaryKeySelective(cartTemp);
            return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", null);
        }else {
            cartService.insert(cart);
            return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", null);
        }
    }

    @DeleteMapping("/delete")
    public ResponseBean delete(@RequestParam Map<String, Object> map){
        Cart cart = new Cart();
        cart.setCompanyId((Integer) map.get("companyId"));
        cart.setStockId((Integer) map.get("stockId"));
        cartService.delete(cart);
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }

    @PutMapping("/update")
    public ResponseBean update(@RequestBody CartDto cartDto){
        cartService.updateByPrimaryKey(cartDto);
        return new ResponseBean(HttpStatus.OK.value(), "修改成功(Update Success)", null);
    }

    @GetMapping("/getMy")
    public ResponseBean get(@RequestParam Map<String, Object> map){
        int page = 1, rows = 10;
        if(map.containsKey("page"))
            page = Integer.parseInt(map.get("page").toString());
        if(map.containsKey("rows"))
            rows = Integer.parseInt(map.get("rows").toString());
        PageHelper.startPage(page, rows);

        Cart cart = new Cart();
        cart.setCompanyId(Integer.parseInt(map.get("companyId").toString()));
        List<Cart> cartList = cartService.select(cart);
        List<CartDto> cartDtoList = new ArrayList<>();
        CartDto cartDto = new CartDto();
        Goods goods;
        Stock stock;
        for (Cart temp:cartList
             ) {
            stock = stockService.selectByPrimaryKey(temp.getStockId());
            goods = goodsService.selectByPrimaryKey(stock.getGoodsId());

            String goodsImgUrlListStr = goods.getGoodsImgUrlList();
            List<String> goodsImgUrlList = JSONObject.parseArray(goodsImgUrlListStr, String.class);

            Map<String, Object> modelMap = JSON.parseObject(stock.getModelMap());

            cartDto.setStockId(temp.getStockId());
            cartDto.setShopId(goods.getShopId());
            cartDto.setShopName(shopService.selectByPrimaryKey(goods.getShopId()).getShopName());
            cartDto.setGoodsId(goods.getGoodsId());
            cartDto.setGoodsName(goods.getGoodsName());
            cartDto.setGoodsImgUrl(goodsImgUrlList.get(0));
            cartDto.setModelMap(modelMap);
            cartDto.setPrice(stock.getPrice());
            cartDto.setUnit(stock.getUnit());
            cartDto.setNumber(temp.getNumber());
            cartDtoList.add(cartDto);
        }
        PageInfo<CartDto> selectPage = new PageInfo<CartDto>(cartDtoList);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("count", selectPage.getTotal());
        result.put("cartList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }
}
